
// var Storage  = require("./libs/storage");
// var Storage = think.require("./libs/storage");

// console.log(Storage);
export default class extends Map {
    
    constructor(){
        super()

        this.info = new Map();
    }

    //加入房间
    join(subscribe, socket){
        if(!this.has(subscribe)){
            this.set(subscribe, new Map())
        }

        if(!this.get(subscribe).has(socket.id)){
            this.get(subscribe).set(socket.id, socket);
            return true;
        }
        return false;
    }

    //离开房间
    leave(socket){
        let id = socket.id;
        var rooms = [];
        this.forEach((sks, roomid)=>{
            if(this.get(roomid).has(id)){
                this.get(roomid).delete(id);
                rooms.push(roomid);
            }
        });
        return rooms;
    }

    //返回房间人数
    online(room, istrue = false){
        if(istrue){
            return this.getOnline(room) || (this.has(room) ? this.get(room).size : 0);
        }else{
            return this.has(room) ? this.get(room).size : 0;
        }
    }
    //设置房间信息
    setInfo(roomid, key, value){
        if(!this.info.has(roomid)){
            this.info.set(roomid, new Map())
        }
        return this.info.get(roomid).set(key, value);
    }
    //得到房间信息
    getInfo(roomid, key){
        if(this.info.has(roomid)){
            return this.info.get(roomid).get(key);
        }
        return undefined;
    }
    //设置房间信息
    setOnline(roomid, value){
        if(!this.info.has(roomid)){
            this.info.set(roomid, new Map())
        }
        return this.info.get(roomid).set("online", value);
    }
    //得到房间信息
    getOnline(roomid){
        if(this.info.has(roomid)){
            return this.info.get(roomid).get("online");
        }
        return undefined;
    }
    // 返回房间
    rooms(socket){
        let id = socket.id;
        var rooms = [];
        this.forEach((sks, roomid)=>{
            if(this.get(roomid).has(id)){
                rooms.push(roomid);
            }
        });
        return rooms;
    }
}